package com.zsh.wanandroid.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.design.widget.TextInputEditText;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.api.ApiService;
import com.zsh.wanandroid.base.BaseActivity;
import com.zsh.wanandroid.bean.LoginResult;
import com.zsh.wanandroid.common.APP;
import com.zsh.wanandroid.http.ApiRequest;
import com.zsh.wanandroid.utils.UIUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author zsh
 * @date 2020/3/12.
 * 功能：Activity登录页
 */
public class LoginActivity extends BaseActivity {

    private ImageView ivBack;
    private TextView tvTitle;
    private ImageView ivMore;
    private TextInputEditText etUsername;
    private TextInputEditText etPassword;
    private Button btnLogin;
    private TextView tvRegister;

    @Override
    public void initPrams(Bundle prams) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView(View view) {
        ivBack = $(R.id.ivBack);
        tvTitle = $(R.id.tvTitle);
        ivMore = $(R.id.ivMore);
        etUsername = $(R.id.etUsername);
        etPassword = $(R.id.etPassword);
        btnLogin = $(R.id.btnLogin);
        tvRegister = $(R.id.tvRegister);
    }

    @Override
    public void setListener() {
        ivBack.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack://返回
                back();
                break;

            case R.id.btnLogin://登录
                login();
                break;

            case R.id.tvRegister://跳转注册页面
                startActivity(RegisterActivity.class);
                break;
        }
    }

    @Override
    public void doBusiness(Bundle savedInstanceState) {
        //设置标题
        tvTitle.setText("登录");
        ivMore.setVisibility(View.GONE);
    }

    private void back() {
        self.finish();
    }

    private void login() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("登录中...");
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        if (username.equals("")) {
            UIUtils.showToast("输入用户名");
            return;
        }
        if (password.equals("")) {
            UIUtils.showToast("输入密码");
            return;
        }
        retrofitHttp(username, password);
    }

    private void retrofitHttp(String username, String password) {
        progressDialog.show();
        ApiService loginService = ApiRequest.getInstance(self).createLogin(ApiService.class);
        Call<LoginResult> loginCall = loginService.login(username, password);
        loginCall.enqueue(new Callback<LoginResult>() {
            @Override
            public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
                progressDialog.dismiss();
                LoginResult loginResult = response.body();
                String errorMsg = loginResult.getErrorMsg();
                if (loginResult.getData() != null) {
                    //设置登录成功状态
                    SPUtils.getInstance(APP.currentAppName).put(APP.isLogin, true);
                    UIUtils.showToast("登录成功");
                    Intent i = new Intent();
                    i.putExtra("result", loginResult.getErrorCode());
                    setResult(RESULT_OK, i);
                    self.finish();
                } else {
                    UIUtils.showToast(errorMsg);
                }
            }

            @Override
            public void onFailure(Call<LoginResult> call, Throwable t) {
                progressDialog.dismiss();
            }
        });
    }

    @Override
    public void onBackPressed() {
        back();
    }
}
